package com.hzzc.core.service;

import com.hzzc.BizException;
import com.hzzc.core.dao.SSOApplicationMapper;
import com.hzzc.core.model.SSOApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 后台用户服务
 */
@Service
public class ApplicationService {

    @Autowired
    private SSOApplicationMapper ssoApplicationMapper;

    /**
     * 根据Id倒叙排序
     *
     * @return
     */
    public List<SSOApplication> applicationList() {
        Example example = new Example(SSOApplication.class);
        example.orderBy("id").desc();
        return ssoApplicationMapper.selectByExample(example);
    }


    /**
     * 增加新应用
     *
     * @param name
     * @param code
     * @param msg
     * @throws BizException
     */
    public void addApplication(String name, String code, String msg) throws BizException {
        SSOApplication ssoApplication = new SSOApplication();
        ssoApplication.setCode(code);
        ssoApplication.setName(name);
        ssoApplication.setMsg(msg);
        ssoApplicationMapper.insert(ssoApplication);
    }

    /**
     * 更具appCode获取数据
     *
     * @param appCode
     * @return
     */
    public SSOApplication selectByCode(String appCode) {
        Example example = new Example(SSOApplication.class);
        example.createCriteria().andEqualTo("code", appCode);
        return ssoApplicationMapper.selectOneByExample(example);
    }

    public void delApplication(Long applicationId) {
        ssoApplicationMapper.deleteByPrimaryKey(applicationId);
    }
}
